<template>
	<view class="addressDeal-content" v-if="beforeLoading">
		<form>
			<view class="cu-form-group">
				<view class="title">收货人</view>
				<input placeholder="请输入收货人" v-model="datas.name"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">手机号码</view>
				<input placeholder="请输入手机号码" type="number" :maxlength="11" v-model="datas.phone"></input>
			</view> 
			<view class="cu-form-group relative arrows response" @tap="toggleTab">
				<view class="title">所在地区</view>
				<input placeholder="点击选择地区" v-model="regionAddress" disabled></input>
			</view>
			<view class="cu-form-group">
				<view class="title">详细地址</view>
				<input placeholder="请输入详细地址" v-model="datas.address" :maxlength="20"></input>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">设为默认收货地址</view>
				<switch class="blueBlack" @change="Switch" :class="datas.isDefault?'checked':''" :checked="datas.isDefault" ></switch>
			</view>
			<view class="flex justify-center align-center margin-top" @click="toggleModal" v-if="datas.id!='00000000-0000-0000-0000-000000000000'">
				<view class="bg-white delete-icon round flex-around shadow justify-center align-center" style="width: 80rpx;height: 80rpx;">
					<uni-icons type="trash" size="40" color="#555"></uni-icons>
				</view>
			</view>
		</form>
		<view class="padding margin-top-lg">
			<view class="cu-btn response line-main radius-df" @tap="getWeChatAddress">一键获取微信地址</view>
		</view>
		<view class="padding-lr flex-around cu-bar foot">
			<view class="cu-btn response  bg-blue round" @tap="confirm">保存</view>
		</view>
		<w-picker mode="region" @confirm="onConfirm" ref="picker"></w-picker>
	</view>
</template>

<script>
	import wPicker from "@/components/w-picker/w-picker.vue";
	export default {
		components: {
			wPicker
		},
		data() {
			return {
				beforeLoading:false,//控制页面是否显示
				addressId:0,
				fullRegionCode:'',
				cityList:[],
				isShowCityList:false,//是否显示地址选择列表
				regionAddress:"",
				datas:{
					id:'00000000-0000-0000-0000-000000000000',
					memberId:uni.getStorageSync('userInfo').id,
					isDefault: false,
					name:'',
					phone:'',
					address:'',
					tags:'',
					postalCode:'',
					area:'',
				},
				cityShow:true,// 显示地区选择
				source:0,
				provinceName:'',
				cityName:'',
				countyName:'',
			};
		},
		methods: {
			getDatas() {
				this.$ajax.getUserAddressDetail({id:this.addressId}).then(res=>{
					Object.keys(this.datas).forEach(key => this.datas[key] = res.data[key]);
					this.fullRegionCode=res.data.regionFullIdPath
					this.beforeLoading=true
				})
			},
			Switch(e) {
				this.datas.isDefault = e.detail.value?true:false
			},
			//获取微信地址
			getWeChatAddress(){
				let that=this
				uni.authorize({
				    scope: 'scope.address',
				    success() {
						console.log(1111);
						uni.chooseAddress({
							success: (res) => {
								
								let data={
									province:res.provinceName,
									city:res.cityName,
									county:res.countyName
								}
								console.log(data);
								that.$ajax.GetAreasIdByName(data).then(res2=>{
									that.datas.name=res.userName
									that.datas.phone=res.telNumber
									that.datas.area=res2.data.areasId
									that.datas.address=res.detailInfo
									that.regionAddress=res.provinceName+res.cityName+res.countyName
									that.provinceName=res.provinceName
									that.cityName=res.cityName
									that.countyName=res.countyName
								})
								console.log(res,'微信地址')
							},
							fail: (err) => {
								console.log(err,'err')
							}
						})
				    }
				})
				
			},
			confirm() {
				if(this.datas.name == ''){
					this.$apis.msg("请输入收货人姓名！");
					return false;
				}
				if(!this.$apis.checkPhone(this.datas.phone)) {
					this.$apis.msg('请输入正确的手机号码！')
					return;
				}
				if(this.datas.area == ''){
					this.$apis.msg("请选择地址区域！");
					return false;
				}
				if(this.datas.address == ''||!this.datas.address){
					this.$apis.msg("请输入详细地址！");
					return false;
				}
				let dataA={
					id:this.addressId,
					provinceName:this.provinceName,
					cityName:this.cityName,
					countyName:this.countyName,
					...this.datas
				}
				console.log(this.datas);
				this.$ajax.insertreceiving({...this.datas}).then(res=>{
					// this.$apis.msg("操作成功！",1500,true)
					// if(this.source==1){
					// 	// this.$apis.lastPage().address=res.data
					// 	// this.$apis.lastPage().addressId=res.data.id
					// 	// this.$apis.lastPage().address.reginPathStr = this.regionAddress+this.datas.address;
					// }
					uni.showToast({
						title:res.message,
						icon:"none",
						duration:2000
					})
					setTimeout(()=>{
						uni.navigateBack();
					},800)
				})
			},
			cityChange(e) {
				console.log(e)
				this.provinceName = e.province.shortName
				this.cityName = e.city.shortName
				this.countyName = e.area.shortName
				this.regionAddress =(e.street)? (e.province.shortName +' '+ e.city.shortName +' '+  e.area.shortName +' '+ e.street.shortName) : ((e.area)?(e.province.shortName +' '+  e.city.shortName +' '+  e.area.shortName):(e.province.shortName +' '+  e.city.shortName));
				this.datas.area=String((e.street)? (e.street.area) : ((e.area)?(e.area.area):(e.city.area)));
			} ,
			//地址弹窗显示
			toggleTab(){
				this.$refs.picker.show();
			},
			onConfirm(val){
				this.datas.area=val.checkVal
				this.regionAddress=val.checkArr
			},
			//初始化地址区域回显
			initRegion(e){
				this.regionAddress=e
			},
			toggleModal() {
				uni.showModal({
					title: '温馨提示',
					content: '您确认删除该收货地址信息吗？',
					success:(res)=> {
						if (res.confirm) {
							this.$ajax.receivingdelete({ids:this.datas.id}).then(res=>{
								this.$apis.msg('删除成功!');
								setTimeout(()=>{
									uni.navigateTo({
										url:'/pages/home/addressList/addressList'
									})
								},800)
								
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			handleClick(e) {
				let index = e.index;	
				if (index === 1) {
					this.$apis.deleteAddress({ids:[this.addressId]}).then(res=>{
						this.$unis.msg('删除成功!');
						setTimeout(()=>{
							uni.navigateBack()
						},800)
						
					})
				}
				this.toggleModal()
			}
		},
		onLoad(e) {
			let data=JSON.parse(e.data)
			for(let key in data){
				this.datas[key]=data[key]
			}
			this.regionAddress=data.area
			this.datas.area=data.district
			// this.addressId = e.id?e.id:'';
			if(this.addressId){
				this.getDatas()
			}else{
				this.beforeLoading=true
			}
			
		}
	}
</script>

<style lang="scss">
	.cu-form-group .title {
		min-width: calc(4em + 30px);
	}
	.delete-box{
		margin: 20rpx 0;
		display: flex;
		justify-content: space-around;
		.delete-icon{
			width: 80rpx;
			height: 80rpx;
		}
	}
.addressDeal-content{
	position: relative;
	&>image{
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 6upx;
	}
}
.address-list{
	position: absolute;
	right: 0;
	top: 100upx;
	width: 100%;
	z-index: 99;
	.address-list-item{
		min-width: 0;
		border-bottom: 1px solid #eee;
		padding: 20upx 30upx;
	}
}
</style>
